package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Address;
import com.entity.Admin;
import com.entity.Cake;
import com.entity.Consumer;
import com.entity.Order;
import com.google.gson.Gson;
import com.servers.AddressServers;
import com.servers.AdminService;
import com.servers.CakeServers;
import com.servers.ConsumerServers;
import com.servers.OrderServers;

@Controller
public class AdminController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private CakeServers cakeServers;
	@Autowired
	private OrderServers orderServes;
	@Autowired
	private ConsumerServers consumerServers;
	@Autowired
	private AddressServers addressServers;
	@RequestMapping("/adminlogin")
	public ModelAndView adminLogin(String username,String password,HttpServletRequest request) {
		ModelAndView mvAndView=new ModelAndView();
		Map<String, Object> adminLogin = adminService.adminLogin(username, password);
		if (adminLogin.get("result")!=null) {
			Admin admin=(Admin) adminLogin.get("admin");
			request.getSession().setAttribute("admin", admin.getAdminname());// 将管理员姓名存入到session中
			mvAndView.setViewName("../../adminIndex");
		}else {
			mvAndView.setViewName("../../admin-login");
			mvAndView.addObject("fail", "用户名不存在或者密码错误,请重新输入");
		}
		return mvAndView;
	}
	@RequestMapping("/goadminlogin")
	public String goadminlogin() {
		return "../../admin-login";
	}
	@ResponseBody
	@RequestMapping("/getAllCakes")
	public String goadminCakejsp(HttpServletRequest request) {
		Map<String, Object> map=new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", 11);
		List<Map<String, Object>> list=new ArrayList<>();
		
		String page=request.getParameter("page");
		String limit=request.getParameter("limit");
		int epage=-1;
		int elimit=-1;
		if (page!=null && limit!=null) {
			epage=Integer.parseInt(page);
			elimit=Integer.parseInt(limit);
		}
		List<Cake> allCakes = cakeServers.findCakesLimit((epage-1)*elimit, elimit);
	
		for (Cake cake : allCakes) {
			Map<String, Object> tempmap=new HashMap<>();
			tempmap.put("id", cake.getCakeId());
			tempmap.put("cakename", cake.getCakeName());
			tempmap.put("cakeprice", cake.getCakePrice());
			tempmap.put("cakesales", cake.getCakeSales());
			tempmap.put("cakecontext", cake.getCakeContext());
			int id=cake.getCaketypeId();
			if (id==2 || id==3 || id==4 || id==5) {
				tempmap.put("type", "蛋糕");
			}else if (id==6) {
				tempmap.put("type", "吐司");
			}else if (id==7) {
				tempmap.put("type", "礼品");
			}
			list.add(tempmap);
		}
		map.put("data", list);
		map.put("count", 65);
		Gson gson=new Gson();
		System.out.println("-------------------------------------------------------");
		System.out.println(gson.toJson(map));
		return gson.toJson(map);
	}
	@ResponseBody
	@RequestMapping("/adminOrder")
	public  String  getOrderInfo(HttpServletRequest request){
		List<Map<String, Object>> list=new ArrayList<>();
		String  limit=request.getParameter("limit");
		String  page=request.getParameter("page");
		int epage=-1;
		int elimit=-1;
		if (page!=null && limit!=null) {
			epage=Integer.parseInt(page);
			elimit=Integer.parseInt(limit);
		}
		Map< String, Object> map=new HashMap<>();
		List<Order> olist= orderServes.getOrderInfoBylimit((epage-1)*elimit, elimit);
		List<Order> sizeList=orderServes.getOrderInfo();
		for (Order order : olist) {
			Consumer orderCname=order.getConsumer();//获取订单中的cid;
			String  orderAddress=order.getOaddress();//获得订单中的地址名称
			Consumer consumer=consumerServers.getConsumerInfo(orderCname.getCid(),orderAddress);//通过cid和地址查询用户信息
			Address address =addressServers.getAddressName(order.getConsumer().getCid(),orderAddress);
			System.out.println("consumer#############"+consumer);
			System.out.println("address**************"+address);
			//System.out.println("444444444444444444:"+consumer.getCname());
			Map<String,Object>map1=new HashMap<>();
			map1.put("id",order.getOdiscount());//订单编号
			map1.put("name", order.getCake().getCakeName());//商品名称
			//收货人姓名
			if( consumer!=null){					
				System.out.println("--------进入consumer1"+address);
				map1.put("consumerName",consumer.getCname() );
			}else{
				System.out.println("--------进入consumer2"+consumer);
				map1.put("consumerName",address.getName());								
			}					
			//订单中用户的名称
			map1.put("Address",order.getOaddress());//用户收货地址
			map1.put("SinglePrice", order.getOunitPrice());//封装商品单价
			map1.put("sum",order.getOcost());//同种商品的小计
			map1.put("onum",order.getOnum());//单个商品的数量
			map1.put("orderDate", order.getOdelDate());//订单生成时间
			if(order.getStatus()==0){
				map1.put("status","未发货");
			}else if(order.getStatus()==3){
				map1.put("status","已发货");
			}else{
				map1.put("status","正在处理");
			}
			
			list.add(map1);		
		}
		
		map.put("code",0);
		map.put("msg","");
		map.put("count",sizeList.size());
		map.put("data",list);
		Gson gson=new Gson();
		return  gson.toJson(map);
	}
	@ResponseBody
	@RequestMapping("/searchOrder")
	public String getAdminService(String startTime,String endTime,int orderId) {
		System.out.println("------------------");
		/*System.out.println(startTime+"==="+endTime+"==="+orderId);
		String  start=null;
		String  end=null;
		if(startTime!=null){
			start=startTime;
		}
		if(endTime!=null){
			end=endTime;
		}*/
		List<Map<String, Object>> list=new ArrayList<>();
		Map< String, Object> map=new HashMap<>();
		Map<String,Object>map1=new HashMap<>();
		List<Order> olist=new ArrayList<>();
		if(orderId != 0){
			System.out.println("-----------进入按id查询------------");
			olist=orderServes.getOrderByOrderId(orderId);
		}
		for (Order order : olist) {
			Consumer orderCname=order.getConsumer();//获取订单中的cid;
			String  orderAddress=order.getOaddress();//获得订单中的地址名称
			Consumer consumer=consumerServers.getConsumerInfo(orderCname.getCid(),orderAddress);//通过cid和地址查询用户信息
			Address address =addressServers.getAddressName(order.getConsumer().getCid(),orderAddress);
			//收货人姓名
			if( consumer!=null){					
				System.out.println("--------进入consumer1"+address);
				map1.put("consumerName",consumer.getCname() );
			}else{
				System.out.println("--------进入consumer2"+consumer);
				map1.put("consumerName",address.getName());								
			}	
			map1.put("id",order.getOdiscount());//订单编号
			map1.put("name", order.getCake().getCakeName());//商品名称
			map1.put("Address",order.getOaddress());//用户收货地址
			map1.put("SinglePrice", order.getOunitPrice());//封装商品单价
			map1.put("sum",order.getOcost());//同种商品的小计
			map1.put("onum",order.getOnum());//单个商品的数量
			map1.put("orderDate", order.getOdelDate());//订单生成时间
			map1.put("status","未发货");
			//map1.put("consumerName","aaa");
			System.out.println("------olist的长度:"+olist.size());
			list.add(map1);	
		}
		System.out.println(list.size()+"!!!!!!000000000000000000000");
		map.put("code",0);
		map.put("msg","");
		map.put("count",olist.size());
		map.put("data",list);
		Gson gson=new Gson();
		return  gson.toJson(map);
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	public CakeServers getCakeServers() {
		return cakeServers;
	}
	public void setCakeServers(CakeServers cakeServers) {
		this.cakeServers = cakeServers;
	}
	public void setOrderServes(OrderServers orderServes) {
		this.orderServes = orderServes;
	}
	public void setConsumerServers(ConsumerServers consumerServers) {
		this.consumerServers = consumerServers;
	}
	
}
